<?php
//All the post displaying shtuff
function the_posts() {
	$page = $_GET['p'];
	$post = $_GET['id'];
	global $db_prefix;
	$limitquery = "SELECT value FROM `".$db_prefix."main` WHERE `option` = 'postlimit'";
	$limitresult = mysql_query($limitquery);
	$limit = mysql_fetch_row($limitresult);
	if(isset($post)) {
	$postquery  = "SELECT * FROM ".$db_prefix."posts WHERE id = '$post' LIMIT 1;";
	$postresult = mysql_query($postquery) or die('No post found.'); {
	if(mysql_num_rows($postresult) == 0) {
        echo("That post does not exist!");
    } 
    else {
		while($postrow = mysql_fetch_row($postresult)) {
			$id = $postrow[0];
			$title = $postrow[1];
			$author = $postrow[2];
			$date = $postrow[3];
			$type = $postrow[4];
			$text = $postrow[5];
			if(file_exists(skin_dir()."/$type"."post.php")) {include(skin_dir()."/$type"."post.php");}
			else {ec("No template for $type posts.  Blame the skinner.");}
		}
	}
	}
	}
	else {
	if(isset($page) && is_numeric($page)) {
		$page = $_GET['p'];
	}
	else {
		$page = 1;
	}
	$pagelimit = $page * $limit[0] - $limit[0];
	$postquery  = "SELECT * FROM ".$db_prefix."posts ORDER BY id DESC LIMIT $pagelimit, $limit[0];";
	$postresult = mysql_query($postquery) or die('Not a valid page! Go to the <a href="./">first page</a>.'); {
	if(mysql_num_rows($postresult) == 0) {
        echo("Nothing to Display!");
    } 
    else {
		while($postrow = mysql_fetch_row($postresult)) {
			$title = $postrow[1];
			$author = $postrow[2];
			$date = $postrow[3];
			$type = $postrow[4];
			$text = $postrow[5];
			if(file_exists(skin_dir()."/$type"."post.php")) {include(skin_dir()."/$type"."post.php");}
			else {ec("No template for $type posts.  Blame the skinner.");}
		}
	}
}
}
}
//Next page and previous page
function page_links() {
	global $db_prefix;
	$limitquery = "SELECT value FROM `".$db_prefix."main` WHERE `option` = 'postlimit'";
	$limitresult = mysql_query($limitquery);
	$limit = mysql_fetch_row($limitresult);
	$query = "SELECT count(*) FROM ".$db_prefix."posts ORDER BY date DESC";
	$result = mysql_query($query);
	$count = mysql_fetch_row($result);
	$page = $_GET['p'];
	if(empty($_GET['id'])) {
	if(empty($page)) {$page = 1;}
	$numPages = ceil($count[0]/$limit[0]);
	if(is_numeric($page) && $page > 1) {
		$page--;
		echo "<div class=\"alignleft\"><a href=\"?p=$page\">Previous Page</a></div>";
		$page++;
	}
	if(is_numeric($page) && $page+1 <= $numPages) {
		$page++;
		echo "<div class=\"alignright\"><a href=\"?p=$page\">Next Page</a></div>";
	}
	}
}
//Postform
function postform($type) {
global $db_prefix;
$query = "SELECT value FROM `".$db_prefix."main` WHERE `option` = 'user'";
$result = mysql_query($query);
$data = mysql_fetch_row($result);
	if($type == "text") {
		echo '<form id="postform" method="post" action="?post=1">
    <div class="formtitle">Title</div>
    <input type="text" id="posttitle" cols="80" name="title" /><br />
    <div class="formtitle">Text</div>
    <textarea cols="50" rows="20" name="text" class=\"wys\"></textarea>
    <input type="submit" value="Post" class="postbutton" />
    <input type="hidden" name="type" value="'.$type.'" />
    <input type="hidden" name="author" value="'.$data[0].'" />
  </form>';
	}
	elseif($type == "quote") {
		echo '<form id="postform" method="post" action="?post=1">
    <div class="formtitle">Author</div>
    <input type="text" id="posttitle" cols="80" name="author" /><br />
    <div class="formtitle">Quote</div>
    <textarea cols="50" rows="20" name="text"></textarea>
    <input type="submit" value="Post" class="postbutton" />
    <input type="hidden" name="type" value="'.$type.'" />
  </form>';
	}
	elseif($type == "link") {
		echo '<form id="postform" method="post" action="?post=1">
    <div class="formtitle">Title</div>
    <input type="text" id="posttitle" cols="80" name="title" /><br />
    <div class="formtitle">The link (include http://)</div>
    <input type="text" id="posttitle" cols="80" name="text" class="link" />
    <input type="submit" value="Post" class="postbutton" />
    <input type="hidden" name="type" value="'.$type.'" />
  </form>';
	}
	elseif($type == "image") {
		echo '<form id="postform" method="post" action="?post=1">
    <div class="formtitle">Title</div>
    <input type="text" id="posttitle" cols="80" name="title" /><br />
    <div class="formtitle">The link to the image (include http://)</div>
    <input type="text" id="posttitle" cols="80" name="text" class="link" />
    <input type="submit" value="Post" class="postbutton" />
    <input type="hidden" name="type" value="'.$type.'" />
  </form>';
	}
}
function editform($type) {
global $db_prefix;
$type = $_GET['type'];
if(empty($type) || empty($_GET['id'])) { echo '<a href="./manage.php">No post selected</a>';}
else {
$query3  = "SELECT * FROM $db_prefix"."posts WHERE `id` =".$_GET['id'].";";
$result3 = mysql_query($query3);
while($row3 = mysql_fetch_row($result3)) {
$id = $row3[0];
$title = $row3[1];
$author = $row3[2];
$date = $row3[3];
$type = $row3[4];
$text = $row3[5];
}
  if($type == "text") {
		echo '<form id="postform" method="post" action="?id='.$id.'&update=1">
    <div class="formtitle">Title</div>
    <input type="text" id="posttitle" cols="80" name="title" value="'.$title.'" /><br />
    <div class="formtitle">Text</div>
    <textarea cols="50" rows="20" name="text" class="wys">'.$text.'</textarea>
    <input type="submit" value="Edit" class="postbutton" />
    <input type="hidden" name="id" value="'.$id.'" />
  </form>';
	}
	elseif($type == "quote") {
		echo '<form id="postform" method="post" action="?update=1">
    <div class="formtitle">Author</div>
    <input type="text" id="posttitle" cols="80" name="author" value="'.$author.'" /><br />
    <div class="formtitle">Quote</div>
    <textarea cols="50" rows="20" name="text" class="wys">'.$text.'</textarea>
    <input type="submit" value="Edit" class="postbutton" />
    <input type="hidden" name="id" value="'.$id.'" />
  </form>';
	}
	elseif($type == "link") {
		echo '<form id="postform" method="post" action="?update=1">
    <div class="formtitle">Title</div>
    <input type="text" id="posttitle" cols="80" name="title" value="'.$title.'" /><br />
    <div class="formtitle">The link (include http://)</div>
    <input type="text" id="posttitle" cols="80" name="text" class="link" value="'.$text.'" />
    <input type="submit" value="Edit" class="postbutton" />
    <input type="hidden" name="id" value="'.$id.'" />
  </form>';
	}
	elseif($type == "image") {
		echo '<form id="postform" method="post" action="?update=1">
    <div class="formtitle">Title</div>
    <input type="text" id="posttitle" cols="80" name="title" value="'.$title.'" /><br />
    <div class="formtitle">The link to the image (include http://)</div>
    <input type="text" id="posttitle" cols="80" name="text" class="link" value="'.$text.'" />
    <input type="submit" value="Edit" class="postbutton" />
    <input type="hidden" name="id" value="'.$id.'" />
  </form>';
}
}
}
?>
